package cn.bdqn.controller;

import cn.bdqn.pojo.Role;
import cn.bdqn.pojo.User;
import cn.bdqn.service.RoleService;
import cn.bdqn.service.UserService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {
    @Resource
    RoleService roleService;

    @Resource
    UserService userService;

    /**
     * 列表展示
     * @param usrName
     * @param roleId
     * @param pageIndex
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String list(String usrName,Long roleId,
                       @RequestParam(defaultValue = "1") Integer pageIndex,
                       Model model){
        System.out.println("username:"+usrName+",roleId:"+roleId);
            //设置每页显示条数
        int pageSize=3;
            //查询每页显示的数据
        IPage<User> userPager = userService.getUsersByPage(usrName,roleId,pageIndex,pageSize);
        model.addAttribute("userPager",userPager);
            //查询条件 回显
        model.addAttribute("usrName",usrName);
        model.addAttribute("roleId",roleId);

        return "user/list";

    }

    @GetMapping("/add")
    public String add(Model model){
        //查询所有的角色用户角色下拉框的展示
        List<Role> roles = roleService.findAllRoles();
        model.addAttribute("roles",roles);
        return "user/add";
    }
    /**
     * 新增操作
     * @param user
     * @return
     */
    @RequestMapping("/save")
    public String save(User user, HttpSession session){
        User loginUser = (User) session.getAttribute("loginUser");
        user.setUsrFlag(loginUser.getUsrFlag());
        int rs = userService.addUser(user);
        if(rs>0){
                //跳转到用户列表页面
            return "redirect:/user/list";
        }else{
            //跳转到用户新增页面
            return "redirect:/user/add";
        }

    }

    @RequestMapping("/edit")
    public String edit(Long usrId,Model model){
        User user = userService.selectOne(usrId);
        model.addAttribute("user",user);

        //查询所有的角色用户角色下拉框的展示
        List<Role> roles = roleService.findAllRoles();
        model.addAttribute("roles",roles);
        return "user/edit";
    }


    @RequestMapping("/update")
    public String update(User user){
            //根据id查询用户信息
        int rs=userService.update(user);
        if(rs>0){
            //跳转到用户列表页面
            return "redirect:/user/list";
        }else{
            //跳转到用户修改页面
            return "redirect:/user/edit";
        }
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Map<String,Object> delete(Long usrId,Model model){
        Map<String,Object> map=new HashMap<>();

        int rs = userService.deleteUser(usrId);
        if(rs>0){
            map.put("delResult",true);
        }else{
            map.put("delResult",false);
        }
        return map;
    }
}
